<!--
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-18 18:25:38
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-23 11:35:25
 * @FilePath: /webServerAdmin/src/views/system/email/components/sendEmail.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-drawer v-model="visible" title="发送邮件" size="48vw">
    <el-form ref="formRef" label-position="top" :model="modelValue" :rules="rules">
      <el-form-item label="邮件接收者" prop="receiver">
        <el-input v-model="modelValue.receiver"></el-input>
      </el-form-item>
      <el-form-item label="邮件标题" prop="title">
        <el-input v-model="modelValue.title"></el-input>
      </el-form-item>
      <el-form-item label="邮件主题" prop="subject">
        <el-input v-model="modelValue.subject"></el-input>
      </el-form-item>
      <el-form-item label="邮件正文" prop="context">
        <WangEditor v-model:value="modelValue.context"></WangEditor>
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="sendEmail">发送</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { EmailSchema } from "@/schema";
import { reactive, ref } from "vue";
import WangEditor from "@/components/WangEditor/index.vue";
import { ElNotification, FormInstance } from "element-plus";
import { senEmail } from "@/api/modules/email";
import { ResultEnum } from "@/enums/httpEnum";
import { EmitProps } from "@/typings/interface";
const formRef = ref<FormInstance>();

const modelValue = reactive<EmailSchema>({
  receiver: "",
  title: "",
  subject: "",
  context: ""
});

const rules = {
  receiver: [{ message: "请输入邮件接收者", trigger: "blur", required: true }],
  title: [{ message: "请输入邮件标题", trigger: "blur", required: true }],
  subject: [{ message: "请输入邮件主题", trigger: "blur", required: true }],
  context: [{ message: "请输入邮件正文", trigger: "blur", required: true }]
};

const visible = ref<boolean>(false);
const emits = defineEmits<EmitProps>();

function sendEmail() {
  formRef.value?.validate().then(valid => {
    if (valid) {
      senEmail(modelValue).then(r => {
        const { code, message } = r;
        if (code === ResultEnum.SUCCESS) {
          ElNotification.success("发送成功！");
          visible.value = false;
          emits("success");
        } else {
          ElNotification.error(message);
        }
      });
    }
  });
}

defineExpose({
  open() {
    visible.value = true;
  }
});
</script>
